package com.rdc.jiajiadictionary.notebook;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.rdc.jiajiadictionary.R;
import com.rdc.jiajiadictionary.bean.Word;

import java.io.IOException;


public class NotebookItemActivity extends AppCompatActivity {

    private TextView itemName;
    private TextView itemWordName;
    private TextView itemPh_am;
    private TextView itemPh_en;
    private TextView itemExchange;
    private TextView itemMeans;
    private FrameLayout back;
    private ImageView english;
    private ImageView american;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notebook_item_activity);
        Word word = (Word) getIntent().getExtras().getSerializable("word");
        itemName = (TextView) findViewById(R.id.tv_item);
        itemWordName = (TextView) findViewById(R.id.tv_item_name);
        itemPh_am = (TextView) findViewById(R.id.tv_item_ph_am);
        itemPh_en = (TextView) findViewById(R.id.tv_item_ph_en);
        itemMeans = (TextView) findViewById(R.id.tv_item_means);
        itemExchange = (TextView) findViewById(R.id.tv_item_exchange);
        english = (ImageView) findViewById(R.id.iv_item_english);
        american = (ImageView) findViewById(R.id.iv_item_american);
        back = (FrameLayout) findViewById(R.id.fl_back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        english.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play(word.getPh_en_mp3());
            }
        });
        american.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                play(word.getPh_am_mp3());
            }
        });
        itemName.setText(word.getWord_name());
        itemWordName.setText(word.getWord_name());
        itemPh_am.setText("美: /"+word.getPh_am()+"/ ");
        itemPh_en.setText("英: /"+word.getPh_en()+"/ ");
        itemExchange.setText(word.getExchange());
        itemMeans.setText(word.getPart());
    }
    public void play(String url){

        if(null == mediaPlayer){
            mediaPlayer = new MediaPlayer();
        }
        if(mediaPlayer.isPlaying()){
        }else {
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}